﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using MYNAMESPACE;
using System.Net.Mail;

namespace final_project
{
    public partial class Shffle_money : Form
    {
        private connect_db con;
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();
        public System.Net.NetworkCredential smpt =
            new System.Net.NetworkCredential("hima4105@gmail.com", "0562192218");
        public string nm2;
        public string   passvalue
        {

            get
            {
                return nm2;
            }
            set
            {
                nm2 = value;
            }

        }
       
        public Shffle_money()
        {
            InitializeComponent();
        }
        public void SendEmail(string sendTo, string sendFrom, string subject, string body)
        {
            try
            {
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = smpt;
                client.EnableSsl = true;

                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);

                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                msg.To.Add(to);

                client.Send(msg);



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error");

            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            ID.Text = nm2;
            user US = new user();
           
            US.setnational_id(Int32.Parse(ID.Text.Trim()));
             
            
            con = new connect_db();
            con.sqlquery("select balance from userdata where id=(@namep)");
            con.cmd.Parameters.Add("@namep",US.getnational_id());
            foreach (DataRow dr in con.QueryEx().Rows)
            {

                Balance.Text = dr[0].ToString(); 


            }
             
        
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 

            


            if (Value.Text.Length == 0 || to_ID.Text.Length == 0)
            {
                
                MessageBox.Show("must have value");

            }
            else
            {
                user US = new user();
                user US_to = new user();
                US.setnational_id(Int32.Parse(ID.Text.Trim()));
                US_to.setnational_id(Int32.Parse(to_ID.Text.Trim()));
                US.setbalance(double.Parse(Balance.Text));


                con = new connect_db();
                con.sqlquery("select id from userdata where id=(@namep)");
                con.cmd.Parameters.Add("@namep", US_to.getnational_id());
               int count = 0;
                foreach (DataRow dr in con.QueryEx().Rows){
                               
                   count=count+1;


                }
                if (count == 1 && US.getnational_id() != US_to.getnational_id())
                {

                    if (US.withdrwal(double.Parse(Value.Text)))
                            {
                                con = new connect_db();
                                con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                                con.cmd.Parameters.Add("@namep", US.getnational_id());
                                con.cmd.Parameters.Add("@namep1", US.getbalance());
                                con.sqlnonex();
                                con.sqlquery("select balance from userdata where id=(@namep)");
                                con.cmd.Parameters.Add("@namep", US.getnational_id());
                                foreach (DataRow dr in con.QueryEx().Rows)
                                {

                                    MessageBox.Show("the balance is " + dr[0].ToString());


                                }
                                con = new connect_db();
                                con.sqlquery("select balance from userdata where id=(@namep)");
                                con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                                foreach (DataRow dr in con.QueryEx().Rows)
                                {

                                    to_balance.Text = dr[0].ToString();


                                }
                                US_to.setbalance(double.Parse(to_balance.Text));
                                US_to.deposite(double.Parse(Value.Text.Trim()));
                                con = new connect_db();
                                con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                                con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                                con.cmd.Parameters.Add("@namep1",US_to.getbalance());
                                con.sqlnonex();
                                con.sqlquery("select balance from userdata where id=(@namep)");
                                con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                                foreach (DataRow dr in con.QueryEx().Rows)
                                {

                                    to_balance.Text = dr[0].ToString();


                                    con.sqlquery("select mail from userdata where id=(@namep)");
                                    con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                                    foreach (DataRow dr1 in con.QueryEx().Rows)
                                    {          
                                SendEmail(dr1[0].ToString() + "@gmail.com", "hima4105@gmail.com", "Bank System", "YOUR VALUE BECOME   " + dr[0].ToString() + "    from  id is   " + ID.Text);
                                    }
                                }
                                this.Hide();
                                Account acc = new Account();
                                acc.passvalue = ID.Text;
                                acc.ShowDialog();

                            }
                            else
                            {

                                MessageBox.Show("Do not have this value");
                            }
                        
                }
                else { 
                 

                                MessageBox.Show("Error");

                           }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            user US = new user();
           
            US.setnational_id(Int32.Parse(ID.Text.Trim()));
            this.Hide();
            Account acc = new Account();
            acc.passvalue = ID.Text;
            acc.ShowDialog();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void to_ID_Validating(object sender, CancelEventArgs e)
        {


            if (string.IsNullOrEmpty(ID.Text))
            {

                e.Cancel = true;
                ID.Focus();
                errorProvider1.SetError(ID, "ID Must Be Entered !");

            }

            else if (!Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(ID, "Valid ID 4 Number");

            }



            else if (Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(ID, "");

            }
            else { }




        }

        private void Value_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Value.Text))
            {

                e.Cancel = true;
                Value.Focus();
                errorProvider1.SetError(Value, "Value Must Be Entered !");

            }
            else if (!Regex.IsMatch(Value.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Value, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Value, "");
            }


        }
    }
}
